import axios from 'axios'
import qs from 'qs'
// import Native from '@native'

axios.defaults.timeout = 60000 // 响应时间
axios.defaults.headers.post['Content-Type'] = 'application/x-www-form-urlencoded' // 配置请求头
// axios.defaults.baseURL = '192.168.0.98:8884'
// 添加请求拦截器(POST传参序列化)
axios.interceptors.request.use(
    async (config) => {
        // 在发送请求之前做某件事
        
        const options = {
            ...config.data,
            ...config.params,
        }
        let authToken = window.localStorage.getItem('authToken')
        authToken && (config.headers.authToken = authToken)
        // config.headers.authToken = "eyJhbGciOiJIUzUxMiJ9.eyJleHAiOjE1NjI4MjY2MTMsInVzZXIiOnsiaWQiOjksInVzZXJJZCI6IkNVMjAxOTA1MjAyMDExMTYwMDAwMDAwMDEiLCJ1c2VyVHlwZSI6MCwidXNlckltZ1VybCI6Imh0dHA6Ly95dW4ubGlua3dpbm5pbmcuY29tL3VwbG9hZC91c2VyL2ltZy8xNTYyMjE5MTQyMDA4MDY0OTQwLmpwZWciLCJuaWNrTmFtZSI6Iua1i-ivlTExIiwicmVhbE5hbWUiOm51bGwsImNlbGwiOiIxMjM0NTY3ODkxMSIsInJlZ2lvbkNvZGUiOiI4NiIsImdlbmRlciI6Ik4iLCJiaXJ0aERheSI6bnVsbCwic3RhdHVzIjoxLCJyZWdpc3RlclNvdXJjZUlkIjowLCJyZWNvbW1hbmRJZCI6IjAiLCJpZENhcmQiOiIiLCJjb3VudHJ5Ijoi5Lit5Zu9IiwicHJvdmluY2UiOiIiLCJjaXR5IjoiIiwiYXV0aFNpZ24iOiIiLCJzaWduVGlwIjpudWxsLCJnbXRSZWdpc3RlciI6MTU1ODM1NDI3NzAwMCwiZ210TGFzdExvZ2luIjoxNTYyMjIxODEzNzEyLCJsYXN0TG9naW5EZXZpY2UiOm51bGwsImdtdENyZWF0ZSI6MTU1ODM1NDI3NzAwMCwiZ210TW9kaWZpZWQiOjE1NjIyMjE2MjYwMDAsIm1lbW8iOm51bGwsImZhbnNDb3VudCI6bnVsbCwiZm9sbG93ZWQiOiJubyIsImxvZ2luVG9rZW4iOiIifSwiY3JlYXRlZCI6MTU2MjIyMTgxMzcxMn0.FQSaKzVgrZead8UKi-P9VssbshWsxl_Tf9itxlo_wGH0O9dHTk6yPkzcYebsP-yzUW8GRU3UFBP-ItrGRUOvTg"
    
        if (config.method === 'post') {
            if (config.headers['Content-Type'] !== "multipart/form-data") {
                config.data = options
                config.data = qs.stringify(config.data)
            }
        } else if (config.method === 'get') {
            config.params = options
        }
        return config
    },
    () => {
        // eslint-disable-next-line prefer-promise-reject-errors
        return Promise.reject({
            stat: 'err',
            msg: '参数错误'
        })
    });
// 添加响应拦截器
axios.interceptors.response.use(
    (res) => {
        if (res.status === 200) {
            if (res.data.stat === 'ok') return res.data
            // eslint-disable-next-line prefer-promise-reject-errors
            /*if (res.data.errorCode === '20000' && res.data.msg === "token校验失败，请重新登录") {
				let isApp = /WinClient/.test(navigator.userAgent)
				if (isApp) {
					Native.initApi(() => {
						Native.login()
					})
				} else {

				}
			} else {*/
           
            return Promise.reject({
                stat: 'err',
                msg: res.data.msg
            })
            /*}*/
        } else {
            // eslint-disable-next-line prefer-promise-reject-errors
            return Promise.reject({
                stat: 'err',
                msg: '服务出错，请重试'
            })
        }
    },
    (err) => {
        let msg = ''
        if (err.response) {
            if (err.response.status === 404) {
                msg = '服务出错，请稍候再试'
            } else if (err.response.status === 408) {
                msg = '响应超时，请重试'
            } else {
                msg = '服务出错，请稍候再试'
            }
        } else {
            if (err.code === 'ECONNABORTED') {
                msg = '请求超时，请重试'
            } else {
                msg = '网络异常，请稍候重试'
            }
        }
        // eslint-disable-next-line prefer-promise-reject-errors
        return Promise.reject({
            stat: 'err',
            msg: msg
        })
    })

// POST 方法封装  (参数处理)
export const postRequest = (url, params) => {
    return axios({
        method: 'post',
        url: url,
        data: params,
        headers: {
            'Content-Type': 'application/x-www-form-urlencoded'
        }
    })
}

// POST 方法封装  (文件上传)
export const uploadFileRequest = (url, params) => {
    return axios({
        method: 'post',
        url: url,
        data: params,
        headers: {
            'Content-Type': 'multipart/form-data'
        }
    })
}

//  GET 方法封装
export const getRequest = (url) => {
    return axios({
        method: 'get',
        url: url
    })
}

export const fetch = (url, params, method, _this, config) => {
    return new Promise((resolve, reject) => {
        axios[method](url, params, config).then(res => {
            if (_this) {
                _this.initOk = true
            }
            resolve(res)
        }, err => {
            if (_this) {
                _this.initOk = true
            }
            // Native.showToast(err.msg, 3)
            
            reject(err)
        })
    })
}
